package de.openkanban.client.ui.adminpanel;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;

import de.openkanban.client.login.LogIn;
import de.openkanban.client.ui.presenter.PreparedRequest;
import de.openkanban.client.ui.presenter.UpdateUserAccountRequestCallback;

/**
 * GUI Element for the UserAccountForm
 */
public class UserAccountForm extends DynamicForm {

	public static final String TARGET = "uploadTarget";

	private UpdateUserAccountRequestCallback updateReqCallback;
	private TextItem vorname;
	private TextItem nachname;
	private TextItem namensKuerzel;
	private TextItem email;
	private StaticTextItem message;

	public UserAccountForm(int width) {
		updateReqCallback = new UpdateUserAccountRequestCallback(this);

		RegExpValidator emailValidator = new RegExpValidator();
		emailValidator.setErrorMessage("Invalid email address");
		emailValidator.setExpression("^([a-zA-Z0-9_.\\-+])+@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$");

		vorname = new TextItem();
		vorname.setName("vorname");
		vorname.setTitle("First Name");
		vorname.setRequired(true);

		nachname = new TextItem();
		nachname.setName("nachname");
		nachname.setTitle("Last Name");
		nachname.setRequired(true);

		namensKuerzel = new TextItem();
		namensKuerzel.setName("namensKuerzel");
		namensKuerzel.setTitle("Initials");
		namensKuerzel.setRequired(true);

		email = new TextItem();
		email.setName("email");
		email.setTitle("Email");
		email.setRequired(true);
		email.setValidators(emailValidator);

		HeaderItem headerItem = new HeaderItem("userAccountHeaderItem", "account data");
		headerItem.setValue("account data");

		message = new StaticTextItem();
		message.setTitle("Status:");
		message.setTextAlign(Alignment.LEFT);
		message.setVisible(false);

		setWidth(width);
		setColWidths("40%", "60%");
		setMargin(10);

		setFields(headerItem, vorname, nachname, namensKuerzel, email, message);

		updateView();
	}

	@Override
	public void submit() {
		if (validate()) {
			fillModel();
			PreparedRequest prepReq = new PreparedRequest("user", "update/account", LogIn.getUserDto());
			prepReq.sendAsyncRequest(updateReqCallback);
		}
	}

	private void fillModel() {
		LogIn.getUserDto().setNachname(nachname.getValueAsString());
		LogIn.getUserDto().setVorname(vorname.getValueAsString());
		LogIn.getUserDto().setNamensKuerzel(namensKuerzel.getValueAsString());
		LogIn.getUserDto().setEmail(email.getValueAsString());
	}

	public void updateView() {
		nachname.setValue(LogIn.getUserDto().getNachname());
		vorname.setValue(LogIn.getUserDto().getVorname());
		namensKuerzel.setValue(LogIn.getUserDto().getNamensKuerzel());
		email.setValue(LogIn.getUserDto().getEmail());
	}

	public void showSuccessMessage() {
		message.setValue("update successful!");
		message.setTextBoxStyle("successMessage");
		message.updateState();
		message.show();
	}

	public void showFailureMessage() {
		message.setValue("update failed!");
		message.setTextBoxStyle("failedMessage");
		message.updateState();
		message.show();
	}
}
